package com.xhs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.xhs.constant.RedisKeyEnum;
import com.xhs.service.RedisService;
import com.xhs.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @desc:
 * @projectName: spring-boot-redis
 * @author: xhs
 * @date: 2024-06-21 021 14:45
 * @version: JDK 1.8
 */
@Slf4j
@Service
public class RedisServiceImpl implements RedisService {

    @Resource
    private RedisUtil redisUtil;


    @Override
    public <T> boolean saveToString(RedisKeyEnum key, T t) {
        return redisUtil.set(key.toString(), t);
    }

    @Override
    public <T> boolean saveToString(RedisKeyEnum key, T t, long expireTime) {
        return redisUtil.set(key.toString(), t, expireTime);
    }

    @Override
    public <V> boolean saveToMap(RedisKeyEnum key, Map<String, V> t) {
        return redisUtil.hmset(key.toString(), t);
    }

    @Override
    public <V> boolean saveToMap(RedisKeyEnum key, Map<String, V> t, long expireTime) {
        return redisUtil.hmset(key.toString(), t, expireTime);
    }

    @Override
    public List<String> getKey() {
        return redisUtil.getKey();
    }

    @Override
    public String getString(RedisKeyEnum keyEnum) {
        Object o = redisUtil.get(keyEnum.toString());
        if (o != null) {
            return o.toString();
        }
        return null;
    }

    @Override
    public <T> T getFromString(RedisKeyEnum keyEnum, Class<T> clazz) {
        return redisUtil.getBean(keyEnum.toString(), clazz);
    }

    @Override
    public Map<Object, Object> getFromMap(RedisKeyEnum keyEnum) {
        return redisUtil.hmget(keyEnum.toString());
    }

    @Override
    public <T> T getFromMap(RedisKeyEnum keyEnum, String mapKey, Class<T> clazz) {
        Map<Object, Object> map = redisUtil.hmget(keyEnum.toString());
        if (map == null || map.isEmpty() || !map.containsKey(mapKey)) {
            return null;
        }

        Object val = map.get(mapKey);
        if (val == null) {
            return null;
        }

        if (val.getClass().equals(clazz)) {
            return (T) val;
        }

        return JSON.parseObject((byte[]) val, clazz);
    }

    @Override
    public boolean removeFromMap(RedisKeyEnum keyEnum, Object... key) {
        return redisUtil.hdel(keyEnum.toString(), key);
    }

    @Override
    public boolean delete(RedisKeyEnum keyEnum) {
        return redisUtil.del(keyEnum.toString());
    }

    @Override
    public long increment(RedisKeyEnum keyEnum, String keySuffix, long delta, long time, TimeUnit timeUnit) {
        String redisKey = keyEnum.toString();
        if (StrUtil.isNotBlank(keySuffix)) {
            redisKey = keyEnum.append(keySuffix);
        }

        long increment = redisUtil.incr(redisKey, delta);
        if (time > 0) {
            redisUtil.expire(redisKey, time);
        }

        return increment;
    }

}
